package com.epalmpay.commom;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.epalmpay.status.Rescode;
import com.epalmpay.dto.system.ButtonInDTO;
import com.epalmpay.dto.system.ButtonOutDTO;
import com.epalmpay.dto.system.PageDTO;
import com.epalmpay.dto.system.RoleButtonInDTO;
import com.epalmpay.dto.system.RoleButtonOutDTO;
import com.epalmpay.entity.Button;
import com.epalmpay.entity.Page;
import com.epalmpay.entity.RoleButton;
import com.epalmpay.service.management.IButtonService;
import com.epalmpay.service.management.IRoleButtonService;
import com.epalmpay.util.Tools;

@Controller
@RequestMapping(value="/manager/button")
public class ButtonController extends BaseController {
     
	@Resource  
    private IRoleButtonService roleButtonService;  
	@Resource  
    private IButtonService buttonService;  
	 
	
		
	
	
	/**
	 * 根据menuId查询详情
	 * 
	 */
	@RequestMapping(value="/save")
	@ResponseBody
	public RoleButtonOutDTO  saveButton(RoleButtonInDTO menuInDTO){
		RoleButtonOutDTO roleButtonOutDTO=new RoleButtonOutDTO();
		try {  
			RoleButton roleButton=new RoleButton();
			roleButton.setButtonId(menuInDTO.getButtonId());
			roleButton.setRoleId(menuInDTO.getRoleId());
			roleButtonService.save(roleButton);
			roleButtonOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return roleButtonOutDTO;
		} catch (Exception e) { 
			roleButtonOutDTO.setErrorCode(Rescode.FAIL, "处理出现异常");
			e.printStackTrace();
		}
		return null;
	}	
	
	
	/**
	 * 列出所有角色按钮关联数据
	 * 
	 */
	@RequestMapping(value="/roleFhbuttonlistAll")
	@ResponseBody
	public RoleButtonOutDTO  roleFhbuttonlistAll(RoleButtonInDTO menuInDTO){
		RoleButtonOutDTO roleButtonOutDTO=new RoleButtonOutDTO();
		ArrayList<RoleButtonOutDTO> userOutList=new ArrayList<RoleButtonOutDTO>();
		try {  
			List<RoleButton> roleButtonList=roleButtonService.listAll();
			for (int i = 0; i < roleButtonList.size(); i++) {
				RoleButtonOutDTO roleButton=new RoleButtonOutDTO();
				roleButton.setButtonId(roleButtonList.get(i).getButtonId());
				roleButton.setRbId(roleButtonList.get(i).getRbId());
				roleButton.setRoleId(roleButtonList.get(i).getRoleId());
				userOutList.add(roleButton);
				roleButtonOutDTO.setRoleButtonList(userOutList); 
			}
			roleButtonOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return roleButtonOutDTO;
		} catch (Exception e) { 
			roleButtonOutDTO.setErrorCode(Rescode.FAIL, "处理出现异常");
			e.printStackTrace();
		}
		return null;
	}
	
	
	/**
	 * 列出所有按钮
	 * 
	 */
	@RequestMapping(value="/listAll")
	@ResponseBody
	public ButtonOutDTO  listAllButton(RoleButtonInDTO menuInDTO){
		ButtonOutDTO roleButtonOutDTO=new ButtonOutDTO();
		ArrayList<ButtonOutDTO> userOutList=new ArrayList<ButtonOutDTO>();
		try {  
			List<Button> buttonList=buttonService.listAll();
			for (int i = 0; i < buttonList.size(); i++) {
				ButtonOutDTO button=new ButtonOutDTO();
				button.setBz(buttonList.get(i).getBz());
				button.setFhbuttonId(buttonList.get(i).getFhbuttonId());
				button.setName(buttonList.get(i).getName());
				button.setQxName(buttonList.get(i).getQxName());
				userOutList.add(button);
				roleButtonOutDTO.setButtonList(userOutList); 
			}
			roleButtonOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return roleButtonOutDTO;
		} catch (Exception e) { 
			roleButtonOutDTO.setErrorCode(Rescode.FAIL, "处理出现异常");
			e.printStackTrace();
		}
		return null;
	}
	
	
	
	/**
	 * 显示用户分页列表  
	 * 
	 */
	@RequestMapping(value="/buttonPage")
	@ResponseBody
	public ButtonOutDTO  buttonPage(ButtonInDTO buttonInDTO,PageDTO pageDTO){
		ButtonOutDTO buttonOutDTO=new ButtonOutDTO();
		ArrayList<ButtonOutDTO> buttonList=new ArrayList<ButtonOutDTO>();
		try {  
			Button button=new Button();
			//分页公共参数封装
			Page page=Tools.pageEmtity(pageDTO);
			button.setPage(page); 
			button.setName(buttonInDTO.getName()); 
			buttonOutDTO.setPage(page); 
			List<Button> buttonlist=buttonService.list(button);
			for (int i = 0; i < buttonlist.size(); i++) {
				ButtonOutDTO buttonbeen=new ButtonOutDTO();
				buttonbeen.setFhbuttonId(buttonlist.get(i).getFhbuttonId());
				buttonbeen.setName(buttonlist.get(i).getName());
				buttonbeen.setBz(buttonlist.get(i).getBz());
				buttonbeen.setQxName(buttonlist.get(i).getQxName());
				buttonList.add(buttonbeen);
				buttonOutDTO.setButtonList(buttonList);
			}
			buttonOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return buttonOutDTO;
		} catch (Exception e) { 
			buttonOutDTO.setErrorCode(Rescode.FAIL, "处理出现异常");
			e.printStackTrace();
		}
		return null;
	}		
		
	
	
	/**
	 * 保存按钮信息
	 * 
	 */
	@RequestMapping(value="/savebutton")
	@ResponseBody
	public ButtonOutDTO  savebutton(ButtonInDTO buttonInDTO){
		ButtonOutDTO buttonOutDTO=new ButtonOutDTO();
		try {  
			Button button=new Button();
			button.setBz(buttonInDTO.getBz());
			button.setFhbuttonId(this.get32UUID());
			button.setName(buttonInDTO.getName());
			button.setQxName(buttonInDTO.getQxName());
			buttonService.save(button);
			buttonOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return buttonOutDTO;
		} catch (Exception e) { 
			buttonOutDTO.setErrorCode(Rescode.FAIL, "处理出现异常");
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 修改按钮信息
	 * 
	 */
	@RequestMapping(value="/updatebutton")
	@ResponseBody
	public ButtonOutDTO  updatebutton(ButtonInDTO buttonInDTO){
		ButtonOutDTO buttonOutDTO=new ButtonOutDTO();
		try {  
			Button button=buttonService.findById(buttonInDTO.getFhbuttonId());
			button.setBz(buttonInDTO.getBz());
			button.setName(buttonInDTO.getName());
			button.setQxName(buttonInDTO.getQxName());
			buttonService.edit(button); 
			buttonOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return buttonOutDTO;
		} catch (Exception e) { 
			buttonOutDTO.setErrorCode(Rescode.FAIL, "处理出现异常");
			e.printStackTrace();
		}
		return null;
	}
	
	
	/**
	 * 删除按钮信息
	 * 
	 */
	@RequestMapping(value="/deletebutton")
	@ResponseBody
	public ButtonOutDTO  deletebutton(ButtonInDTO buttonInDTO){
		ButtonOutDTO buttonOutDTO=new ButtonOutDTO();
		try {  
			buttonService.delete(buttonInDTO.getFhbuttonId());
			buttonOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return buttonOutDTO;
		} catch (Exception e) { 
			buttonOutDTO.setErrorCode(Rescode.FAIL, "处理出现异常");
			e.printStackTrace();
		}
		return null;
	}
	
	
	
	/**
	 * 根据id查询详情
	 * 
	 */
	@RequestMapping(value="/findById")
	@ResponseBody
	public ButtonOutDTO  findById(ButtonInDTO buttonInDTO){
		ButtonOutDTO buttonOutDTO=new ButtonOutDTO();
		try {  
			Button button=buttonService.findById(buttonInDTO.getFhbuttonId());
			buttonOutDTO.setBz(button.getBz());
			buttonOutDTO.setFhbuttonId(button.getFhbuttonId());
			buttonOutDTO.setName(button.getName());
			buttonOutDTO.setQxName(button.getQxName());
			buttonOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return buttonOutDTO;
		} catch (Exception e) { 
			buttonOutDTO.setErrorCode(Rescode.FAIL, "处理出现异常");
			e.printStackTrace();
		}
		return null;
	}
	
}
